package com.woniu113.junk.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.order.client.UserClient;
import com.woniu113.junk.order.client.dto.AdminInfoDTO;
import com.woniu113.junk.order.client.dto.UserInfoDTO;
import com.woniu113.junk.order.dto.ComplaintDTO;
import com.woniu113.junk.order.dto.ComplaintInfoDTO;
import com.woniu113.junk.order.exception.ComplainException;
import com.woniu113.junk.order.model.ComplaintInfo;
import com.woniu113.junk.order.mapper.ComplaintInfoMapper;
import com.woniu113.junk.order.model.OrderInfo;
import com.woniu113.junk.order.param.AddComplainInfoParam;
import com.woniu113.junk.order.param.FindComplainInfoParam;
import com.woniu113.junk.order.service.ComplaintInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.order.service.OrderInfoService;
import com.woniu113.junk.order.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class ComplaintInfoServiceImpl extends ServiceImpl<ComplaintInfoMapper, ComplaintInfo> implements ComplaintInfoService {
    @Resource
    private ComplaintInfoMapper cim;
    @Resource
    private OrderInfoService ois;
    @Resource
    private ComplaintInfoService cis;
    @Resource
    private UserClient uc;
    @Override
    public void complain(AddComplainInfoParam param) {
        String complaintContent = param.getComplaintContent();
        String orderNum = param.getOrderNum();
        //1.判断传入参数是否正确
        if (param.getComplaintContent()==null||param.getOrderNum()==null) throw new ComplainException("传入参数不能为空",5101);
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo==null) throw new ComplainException("订单不存在",5102);
        QueryWrapper<ComplaintInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_num",orderNum).eq("complaint_state", Data.COMPLAIN_INFO_STATE_WAITING);
        ComplaintInfo complaintInfo = cim.selectOne(queryWrapper);
        if (complaintInfo!=null) throw new ComplainException("订单尚有投诉未处理，请勿重复投诉",5103);
        //2.新增投诉信息
        UserInfoDTO userInfoDTO = uc.selectIntegral(orderInfo.getUserId()).getData();
        ComplaintInfo complaint = new ComplaintInfo();
        complaint.setUserId(orderInfo.getUserId());
        complaint.setUserName(userInfoDTO.getUserName());
        complaint.setUserPhone(userInfoDTO.getUserPhone());
        complaint.setComplaintContent(param.getComplaintContent());
        complaint.setOrderNum(orderNum);
        complaint.setComplaintedId(orderInfo.getRiderId());
        complaint.setComplaintedId(orderInfo.getRiderId());
        complaint.setComplaintState(Data.COMPLAIN_INFO_STATE_WAITING);
        complaint.setComplaintTime(LocalDateTime.now());
        complaint.setState(Data.COMPLAIN_STATE_NORMAL);
        cim.insert(complaint);
        //3.新增日志
    }

    @Override
    public ComplaintDTO find(FindComplainInfoParam param) {
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        //1.根据参数是否不为空拼接查询
        QueryWrapper<ComplaintInfo> queryWrapper = new QueryWrapper<>();
        if (param.getComplaintState() != null) {
            queryWrapper.eq("complaint_state", param.getComplaintState());
        }
        if (param.getOrderNum() != null && !param.getOrderNum().isEmpty()) {
            queryWrapper.eq("order_num", param.getOrderNum());
        }
        if (param.getUserId() != null) {
            queryWrapper.eq("user_id", param.getUserId());
        }
        long count = cim.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<ComplaintInfo> infoList = cim.selectList(queryWrapper);
        ArrayList<ComplaintInfoDTO> list = new ArrayList<>();
        for (ComplaintInfo complaintInfo : infoList) {
            ComplaintInfoDTO complaintInfoDTO = BeanUtil.toBean(complaintInfo, ComplaintInfoDTO.class);
            //查询骑手信息将信息填入
            AdminInfoDTO data = uc.selectAdminById(complaintInfo.getComplaintedId()).getData();
            complaintInfoDTO.setAdminName(data.getAdminName());
            complaintInfoDTO.setAdminPhone(data.getAdminPhone());
            list.add(complaintInfoDTO);
        }
        ComplaintDTO complaintDTO = new ComplaintDTO();
        complaintDTO.setDtos(list);
        complaintDTO.setTotal(Integer.valueOf((int) count));
        return complaintDTO;
    }

    @Override
    public ComplaintDTO findByRider(FindComplainInfoParam param) {
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        //1.根据参数是否不为空拼接查询
        QueryWrapper<ComplaintInfo> queryWrapper = new QueryWrapper<>();
        if (param.getComplaintState() != null) {
            queryWrapper.eq("complaint_state", param.getComplaintState());
        }
        if (param.getOrderNum() != null && !param.getOrderNum().isEmpty()) {
            queryWrapper.like("order_num", param.getOrderNum());
        }
        if (param.getUserId() != null) {//被投诉人
            queryWrapper.eq("complainted_id", param.getUserId());
        }
        long count = cim.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<ComplaintInfo> infoList = cim.selectList(queryWrapper);
        ArrayList<ComplaintInfoDTO> list = new ArrayList<>();
        for (ComplaintInfo complaintInfo : infoList) {
            ComplaintInfoDTO complaintInfoDTO = BeanUtil.toBean(complaintInfo, ComplaintInfoDTO.class);
            //查询骑手信息将信息填入
            AdminInfoDTO data = uc.selectAdminById(complaintInfo.getComplaintedId()).getData();
            complaintInfoDTO.setAdminName(data.getAdminName());
            complaintInfoDTO.setAdminPhone(data.getAdminPhone());
            list.add(complaintInfoDTO);
        }
        ComplaintDTO complaintDTO = new ComplaintDTO();
        complaintDTO.setDtos(list);
        complaintDTO.setTotal(Integer.valueOf((int) count));
        return complaintDTO;
    }
}
